package com.example.dragcontentviewdemo.activity.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

import com.example.dragcontentviewdemo.helper.internal.util.CaculatorUtil;
import com.example.dragcontentviewdemo.helper.protocols.DraggableActivityGestureListener;

import java.util.HashSet;
import java.util.Set;

/**
 * 这是一个demo的ListView，简单实现了DraggableActivityGestureListener#isInterestedIn(MotionEvent event)方法
 */
public class DemoListView extends ListView implements DraggableActivityGestureListener {
    private static final String TAG = "DemoListView";
    private static final float CONSTANT_SLOPE_THRESHOLD = 0.9f; //判定为滚动的斜率阈值

    private Set<Responder> responders = new HashSet<>();
    private MotionEvent downEvent;

    public DemoListView(Context context) {
        super(context);
    }

    public DemoListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DemoListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public void onMoved(float fraction, boolean ltr) {

    }

    @Override
    public void onMoveOutOfBound() {

    }

    //issue to resolved
    @Override
    public boolean isInterestedIn(MotionEvent event) {
        //将此事件通知到订阅view，查看响应
        final int childCount = this.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child.dispatchTouchEvent(event)) { //用了一种比较tricky的方式去完成这种效果
                return true;
            }
        }
        //没有直接子View想要处理，下一步判断。
        //downEvent判断一致性，并防止空指针错误， 子view不消费，则交由斜率判断是否是需要滚动的
        if (downEvent != null) {
            final boolean hasScrollTrend = hasScrollTrend(downEvent, event);
            downEvent.recycle();
            return hasScrollTrend;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
            downEvent = MotionEvent.obtain(ev); //需要拷贝一份这里
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }

    public void addResponder(Responder responder) {
        responders.add(responder);
    }

    interface Responder {
        boolean canReceiveMotionEvent(MotionEvent event);
    }

    private boolean hasScrollTrend(MotionEvent downEvent, MotionEvent firstMoveEvent) {
        final float slope = CaculatorUtil.calculateSlope(
                firstMoveEvent.getRawX(),
                downEvent.getRawX(),
                firstMoveEvent.getRawY(),
                downEvent.getRawY()
        );
        final float positive_slope = Math.abs(slope);
        debugLog("slope = " + positive_slope);
        return positive_slope > CONSTANT_SLOPE_THRESHOLD;
    }

    private void debugLog(String msg) {
        Log.d(TAG, msg);
    }
}
